{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "cefb737e",
   "metadata": {},
   "source": [
    "# 问题描述\n",
    "## 核心问题\n",
    "某社交APP希望增加直播频道的点击转化率\n",
    "## 实验设计\n",
    "1. 产品方案如下：在直播频道的选项卡上增加红点提示\n",
    "2. 实验设计方法如下：\n",
    "> <b>混淆变量</b>：相同用户行为特征的两组用户，主要控制的变量有 <br>\n",
    "    > 视频的观看习惯：观看时长｜观看互动水平，显示为留言量和弹幕量 <br>\n",
    "    > app的使用习惯：使用app的时间段｜用户生命周期处于同一水平 <br>\n",
    "    > 用户的社会学特征：年龄和地域分层抽样，保证AB两组中用户比例与总体一致 <br>\n",
    "> <b>干预</b>：直播频道选项卡上是否出现红点 <br>\n",
    "> <b>目标变量</b>：直播频道点击转化率 <br>\n",
    "3. 样本量选取\n",
    "4. 实验周期： 5个连续日历天\n",
    "5. 构造假设\n",
    "> $H_0$ = AB两组在直播频道点击转化率上没有本质差异 <br>\n",
    "> $H_1$ = AB两组在直播频道点击转化率上有本质差异\n",
    "6. 检验分布： 由于样板量只有5，因此我们使用t分布进行检验函数的构造"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "68e37a81",
   "metadata": {},
   "source": [
    "# 数据处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c1638e6e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2023-09-27</td>\n",
       "      <td>12.796875</td>\n",
       "      <td>12.203125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2023-09-28</td>\n",
       "      <td>12.703125</td>\n",
       "      <td>10.500000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2023-09-29</td>\n",
       "      <td>12.296875</td>\n",
       "      <td>12.203125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2023-09-30</td>\n",
       "      <td>13.796875</td>\n",
       "      <td>11.203125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2023-10-01</td>\n",
       "      <td>11.601562</td>\n",
       "      <td>18.796875</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "        Date          A          B\n",
       "0 2023-09-27  12.796875  12.203125\n",
       "1 2023-09-28  12.703125  10.500000\n",
       "2 2023-09-29  12.296875  12.203125\n",
       "3 2023-09-30  13.796875  11.203125\n",
       "4 2023-10-01  11.601562  18.796875"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import warnings\n",
    "import os\n",
    "\n",
    "warnings.filterwarnings('ignore')\n",
    "test_res = pd.DataFrame({\"Date\": pd.date_range(start='2023-09-27',end='2023-10-01',freq='D'),\n",
    "                         \"A\":np.array([12.8,12.7,12.3,13.8,11.6],dtype=np.float16),\n",
    "                         \"B\":np.array([12.2,10.5,12.2,11.2,18.8],dtype=np.float16)\n",
    "                        })\n",
    "test_res.head(5)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ffdc38ed",
   "metadata": {},
   "source": [
    "# 方差齐性检验\n",
    "1. 采用Levene方差齐性检验，构造的检验统计量本质上是一个组间方差和组内方差的F检验"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "d81c7fce",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.414548215849387 0.10181205725676695\n"
     ]
    }
   ],
   "source": [
    "from scipy.stats import levene\n",
    "\n",
    "w,p = levene(test_res[\"A\"],test_res[\"B\"],center=\"mean\")\n",
    "print(w,p)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "29a57770",
   "metadata": {},
   "source": [
    "由于p>$\\alpha=0.05$，我们认为支持原假设，即实验组和对照组方差是相同的"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c765e410",
   "metadata": {},
   "source": [
    "2. 采用Bartlett方差齐性检验，检验假设两项分布是相互独立且正态的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "d5f5cc75",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5.607297343014178 0.017885828717787555\n"
     ]
    }
   ],
   "source": [
    "from scipy.stats import bartlett\n",
    "\n",
    "w,p = bartlett(test_res[\"A\"],test_res[\"B\"])\n",
    "print(w,p)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "53b2bd2a",
   "metadata": {},
   "source": [
    "由于p<$\\alpha=0.05$，我们认为推翻原假设，即实验组和对照组方差是有差异的"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "cc3c97a5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>A</th>\n",
       "      <th>B</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>5.000000</td>\n",
       "      <td>5.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>12.640625</td>\n",
       "      <td>12.976562</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>0.800293</td>\n",
       "      <td>3.330078</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>11.601562</td>\n",
       "      <td>10.500000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>12.296875</td>\n",
       "      <td>11.203125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>12.703125</td>\n",
       "      <td>12.203125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>12.796875</td>\n",
       "      <td>12.203125</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>13.796875</td>\n",
       "      <td>18.796875</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "               A          B\n",
       "count   5.000000   5.000000\n",
       "mean   12.640625  12.976562\n",
       "std     0.800293   3.330078\n",
       "min    11.601562  10.500000\n",
       "25%    12.296875  11.203125\n",
       "50%    12.703125  12.203125\n",
       "75%    12.796875  12.203125\n",
       "max    13.796875  18.796875"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test_res.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "40a4e386",
   "metadata": {},
   "source": [
    "# 假设检验"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "81dc9deb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "t stats: -0.22343449338074386 , \n",
      "p value: 0.828797244425559\n"
     ]
    }
   ],
   "source": [
    "import statsmodels.stats.api as sms\n",
    "\n",
    "tr = sms.ttest_ind(test_res.A,test_res.B)\n",
    "print(\"t stats: {} , \\np value: {}\".format(tr[0],tr[1]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "0639f91b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-0.22446175163718796 0.8280247967287839\n"
     ]
    }
   ],
   "source": [
    "from scipy.stats import ttest_ind\n",
    "\n",
    "t,p = ttest_ind(test_res[\"A\"],test_res[\"B\"],equal_var=True)\n",
    "print(t,p)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "960094e0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-0.22446175163718796 0.832311005971234\n"
     ]
    }
   ],
   "source": [
    "from scipy.stats import ttest_ind\n",
    "\n",
    "t,p = ttest_ind(test_res[\"A\"],test_res[\"B\"],equal_var=False)\n",
    "print(t,p)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "089dbef5",
   "metadata": {},
   "source": [
    "## 结果分析"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "d3b1a439",
   "metadata": {},
   "source": [
    "结果表明，p值小大于$\\alpha=0.05$，我们没有充分的理由拒绝原假设$H_0$，也就是说明AB两个实验不存在显著差异，<br>因此我们可以得出结论，该产品的优化没有达到预期<br>我们不能认为红点方案对于提升直播频道点击转化率有促进作用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3b400310",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "python3.6",
   "language": "python",
   "name": "python3.6"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "287.594px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
